package org.gpswalker.sys.server.components;

import org.gpswalker.sys.server.misc.Logger;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

public class SVG {

    Document doc;
    Element root;
    Namespace ns;
    int color = 0x0;
//    double radius;
    double width = 1.5;
    int canvasHeight = 0;
    int canvasWidth = 0;
    
    
    
    public void setWidth(double w) {
	width=w;
    }
    
    public void setColor(int c) {
	if (c < 0 || c > 0xffffff) {
	    throw new RuntimeException ("The color you are trying to set is out of the color range");
	}
	color = c;
    }
    
    public SVG(int width, int height) {
	canvasHeight = height;
	canvasWidth = width;
	doc = new Document();
	root = new Element("svg");
	ns = Namespace.getNamespace("http://www.w3.org/2000/svg");
	DocType dt = new DocType("svg", "-//W3C//DTD SVG 1.1//EN",
		"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd");
	doc.setDocType(dt);
	
	// viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice"  
	// style="width:100%; height:100%; position:absolute; top:0; left:0; z-index:-1;">
	
	root.setNamespace(ns);
	root.setAttribute("version", "1.1");
	root.setAttribute("width", "" + width);
	root.setAttribute("height", "" + height);

	doc.addContent(root);
    }

    public void circle(Point center, double r) {
	Element c = new Element("circle", ns);
	c.setAttribute("cx", center.x + "");
	c.setAttribute("cy", center.y + "");
	c.setAttribute("r", r + "");
	c.setAttribute("stroke", toHexString(color));
	c.setAttribute("stroke-width", "" + width);
	c.setAttribute("fill", "none");

	root.addContent(c);
    }

    // <line x1="0" y1="0" x2="300" y2="300"
    // style="stroke:rgb(99,99,99);stroke-width:2"/>

    public void line(Point start, Point end) {
	Element l = new Element("line", ns);
	l.setAttribute("x1", start.x + "");
	l.setAttribute("y1", start.y + "");
	l.setAttribute("x2", end.x + "");
	l.setAttribute("y2", end.y + "");
	l.setAttribute("stroke-width", "" + width);
	l.setAttribute("stroke", toHexString(color));

	root.addContent(l);

    }

    public void lineWithCircles(Point start, Point end, double radius) {

	double deltaX = (end.x - start.x);
	double deltaY = (end.y - start.y);

	double tanAlfa = (deltaX / deltaY);

	double alfa = Math.atan(tanAlfa);

	double incircleX = radius * Math.sin(alfa);
	double incircleY = radius * Math.cos(alfa);

//	circle(start.x, start.y, radius);
//	circle(end.x, end.y, radius);

	double startY;
	double endY;
	double startX;
	double endX;

	if (end.y >= start.y) {
	    startY = start.y + incircleY;
	    endY = end.y - incircleY;
	    startX = start.x + incircleX;
	    endX = end.x - incircleX;
	} else {
	    startY = start.y - incircleY;
	    endY = end.y + incircleY;
	    startX = start.x - incircleX;
	    endX = end.x + incircleX;
	}

	line(new Point(startX, startY), new Point(endX, endY));

	// double newStartX = start.x

    }

    public Document doc() {
	return doc;
    }

    
    public void linearGraph(double[] whies) {
	double factor = 50.0;

	for (int i = 0; i < whies.length - 1; i++) {
	    double x = i * factor + 3.5;
	    circle(new Point (x, canvasHeight - whies[i]), 3.5);
	    lineWithCircles(new Point(x, canvasHeight - whies[i]), new Point((i + 1)
		    * factor + 3.5, canvasHeight - whies[i + 1]), 3.5);
	}
	
	

	circle(new Point ((whies.length - 1) * factor + 3.5, canvasHeight - whies[whies.length - 1]),
		3.5);
    }
    
   public static String toHexString (int i) {
	String internalString = Integer.toHexString(i);
	int difference = 6 - internalString.length();
	for (i=0; i < difference; i++) {
	    internalString = "0" + internalString;
	}
       
       return "#" + internalString;
    }
    
    
    
}